﻿namespace ZyGame.Avatar
{
    using ZyGame.Avatar;
    using System;
    using System.Collections.Generic;

    public sealed class BonesData
    {
        public string location_path;
        public Element position_type;

        public void Dispose()
        {
        }
    }

    /// <summary>
    /// 骨骼配置，表明每个部件挂点的骨骼路径
    /// </summary>
    public sealed class BonesConfig : Singleton<BonesConfig>
    {
        public List<BonesData> configs;
        private void EnsureLoadConfig()
        {
            if (configs != null)
            {
                return;
            }
            configs = new List<BonesData>();
        }

        public void AddConfig(Element element, string bonesPath)
        {
            EnsureLoadConfig();
            RemoveConfig(element);
            BonesData config = new BonesData();
            config.position_type = element;
            config.location_path = bonesPath;
            configs.Add(config);
        }
        public void RemoveConfig(Element element)
        {
            EnsureLoadConfig();
            BonesData config = GetConfig(element);
            if (config == null)
            {
                return;
            }
            configs.Remove(config);
        }
        public BonesData GetConfig(Element element)
        {
            EnsureLoadConfig();
            BonesData config = null;
            for (int i = 0; i < configs.Count; i++)
            {
                if (configs[i].position_type == element)
                {
                    config = configs[i];
                    break;
                }
            }
            if (config.location_path.IsNullOrEmpty())
            {
                return default;
            }
            return config;
        }

        public void Initialized(string config)
        {
            if (string.IsNullOrEmpty(config))
            {
                return;
            }
            configs = Newtonsoft.Json.JsonConvert.DeserializeObject<List<BonesData>>(config);
        }
    }
}